package com.mall.controller;


import com.mall.domain.Cart;
import com.mall.domain.ItemGoods;
import com.mall.service.ICartService;
import com.mall.service.IGoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.Console;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class CartController {
    @Autowired
    private ICartService iCartService;

    //第一次加入购物车
    @RequestMapping("/addCart")
    public  @ResponseBody
    void addCart(Cart cart){
        Map<String, Object> map = new HashMap<>();
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("code",200);
        metaMap.put("msg","插入成功");

        iCartService.addCart(cart);
        map.put("meta",metaMap);
    }

    //多次加入购物车(商品数量+1)/修改购物车商品数量
    @RequestMapping("/addCartNum")
    public @ResponseBody
    void addCartNum(Cart cart){
        System.out.println(cart);
        Map<String, Object> map = new HashMap<>();
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("code",200);
        metaMap.put("msg","插入成功");
        iCartService.addCartNum(cart);
        map.put("meta",metaMap);
    }

    //删除购物车商品
    @RequestMapping("/deleteCartGoods")
    public @ResponseBody
    void deleteCartGoods(Cart cart){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("code",200);
        metaMap.put("msg","删除成功");
        iCartService.deleteCartGoods(cart);
        map.put("meta",metaMap);
    }
    //删除购物车商品
    @RequestMapping("/deleteCartGoods1")
    public @ResponseBody
    void deleteCartGoods1(Cart cart){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("code",200);
        metaMap.put("msg","删除成功");
        iCartService.deleteCartGoods(cart);
        map.put("meta",metaMap);
    }

    //展示购物车中商品数据
    @RequestMapping("/showCart")
    public  @ResponseBody
    Map<String,Object> getGoods() {
        Map<String,Object> map = new HashMap<>();

        //获取购物车商品集合
        Map<String,Object> msgMap = new HashMap<>();
        List<Cart> carts = iCartService.showCart();
        msgMap.put("message",carts);

        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","购物车商品查询成功");
        metaMap.put("status",200);
        msgMap.put("meta",metaMap);

        map.put("message",msgMap);
        map.put("meta",metaMap);
        return map;
    }
}
































